package com.laowang.project.system.controller;

import com.alibaba.fastjson.JSON;

import com.google.gson.Gson;
import com.laowang.common.utils.DateUtils;
import com.laowang.common.utils.http.HttpKit;
import com.laowang.common.utils.security.Md5Utils;
import com.laowang.framework.web.domain.AjaxResult;

import com.laowang.project.system.domain.vo.*;

import lombok.extern.slf4j.Slf4j;


import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.util.*;
import java.util.concurrent.ExecutionException;

/**
 * @Auther laowang
 * @Date 2022/12/12
 * @Description
 */
@Slf4j
//@RestController
public class WanXiangController {

    /**
     *
     * 星pos 回调地址
     *  万象支付回调地址  https://hubeixiangxiang.com/shareweb/wx/gtpNotify
     * @param
     * @param request
     * @return
     */
    //@ApiOperation(value = "前端支付回调参数", notes = "")
    //@PostMapping("/wx/gtpNotify")
    public AjaxResult notify02(HttpServletRequest request) {
        String resXml = "";
        InputStream is = null;
        StringBuilder sb = null;
        try {
            is = request.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "GBK"));
            sb = new StringBuilder();
            String line = null;

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException var18) {
            var18.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException var17) {
                var17.printStackTrace();
            }

        }

        resXml = sb.toString();

        log.info("国通返回的异步通知请求包: {}", resXml);
        Map<String,String> map = JSON.parseObject(resXml, Map.class);
        String txnStatus = map.get("TxnStatus");
        if (txnStatus.equals("1")){
            String txnLogId = map.get("TxnLogId");
            String txnAmt = map.get("TxnAmt");

            Gson gson = new Gson();
            String url ="http://ztopenapi.crland.com.cn/api-gateway/rs-service/";
            String Api_ID="mixc.imPOSWBJB.GLWXCJB.orderCollect";
            String Api_Version="1.0.1";
            String cashierId ="1102a001l0208n0501"; //收银员编号
            String checkCode ="p88888888";
            String store ="L0208N05"; //店铺编号
            String mall ="1102A001"; //商场编号
            String tillId ="01"; //收银机编号
            String App_Pub_ID ="10000187223RL";
            String Partner_ID ="70000029";
            String App_ID ="10000187223IS";  //App_ID即App_Sub_ID
            String signPass ="3c67b01b20a8427d8cb138d7f0e41eea";
            String Token ="630bba0e63f746bd87c016b7796358a0";
            String Sys_ID ="100001872";
            String Sign_Method="md5" ;
            String type ="SALE";
            String orderId = txnLogId;

            PayList payList = new PayList();
            payList.setPayAmt(Integer.valueOf(txnAmt));
            payList.setValue(Integer.valueOf(txnAmt));
            payList.setDiscountAmt(0);
            payList.setPaymentMethod("CH");

            String time = DateUtils.dateTimeNow();
            payList.setTime(time);

            String time1 = DateUtils.getTime1();
            List<PayList> payLists = new ArrayList<>();
            payLists.add(payList);

            List<ItemList> itemLists =new ArrayList<>();

            RequestData requestDataObject = new RequestData();

            requestDataObject.setPayList(payLists);
            requestDataObject.setItemList(itemLists);
            requestDataObject.setCashierId(cashierId); //收银员编号
            requestDataObject.setCheckCode(checkCode);
            //requestDataObject.setComments("test");
            requestDataObject.setMall(mall);//商场编号
            requestDataObject.setOrderId(orderId);
            requestDataObject.setStore(store);//
            requestDataObject.setTillId(tillId); // 收银机编号
            requestDataObject.setTime(time);
            requestDataObject.setTotalAmt(Integer.valueOf(txnAmt));
            requestDataObject.setType(type);

            //String requestData = JSON.toJSONString(requestDataObject);
            String requestData = gson.toJson(requestDataObject);
            //{"cashierId":"0202a501bhsl113n0101","checkCode":"88888888","comments":"test","mall":"0202A501","mobile":"","orderId":"HSS0351001100120041010007","store":"BHSL113N01","tillId":"01","time":"20211214095527","totalAmt":100,"type":"SALE","itemList":[{"itemCode":"BHSL113N0100","price":100,"quantity":1}],"payList":[{"cardBank":"","cardNumber":"","discountAmt":0,"payAmt":100,"paymentMethod":"CH","time":"20211214095526","value":100}]}
            String sign  ="Api_ID="+Api_ID+ "&Api_Version=" +Api_Version+ "&App_Pub_ID="+App_Pub_ID + "&App_Sub_ID="+App_Pub_ID + "&App_Token="+Token+ "&Format=json"+ "&Partner_ID="+Partner_ID+ "&REQUEST_DATA="+requestData+ "&Sign_Method="+Sign_Method+ "&Sys_ID="+Sys_ID+  "&Time_Stamp="+time1+"&"+signPass;
            sign = Md5Utils.hash(sign).toUpperCase();

            RequestObject2 requestObject2 = new RequestObject2();
            requestObject2.setApi_ID(Api_ID);
            requestObject2.setApi_Version(Api_Version);
            requestObject2.setApp_Pub_ID(App_Pub_ID);
            requestObject2.setApp_Token(Token);
            requestObject2.setPartner_ID(Partner_ID);
            requestObject2.setApp_Sub_ID(App_ID);
            requestObject2.setFormat("json");
            requestObject2.setTime_Stamp(time1);
            requestObject2.setSign_Method(Sign_Method);
            requestObject2.setSign(sign);
            requestObject2.setSys_ID(Sys_ID);

            RequestObject1 requestObject1 = new RequestObject1();
            requestObject1.setHRT_ATTRS(requestObject2);

            RequestData requestData1 = new RequestData();
            requestData1.setType(type);
            requestData1.setTotalAmt(Integer.valueOf(txnAmt));
            requestData1.setTime(time);
            requestData1.setTillId(tillId);
            requestData1.setStore(store);
            requestData1.setOrderId(orderId);
            requestData1.setMall(mall);
            requestData1.setCheckCode(checkCode);
            requestData1.setCashierId(cashierId);
            requestData1.setItemList(itemLists);
            requestData1.setPayList(payLists);

            requestObject1.setREQUEST_DATA(requestData1);

            RequestObject requestObject = new RequestObject();
            requestObject.setREQUEST(requestObject1);

            //String str = JSON.toJSONString(requestObject);
            //String str = JSONObject.toJSONString(requestObject);
            String str = gson.toJson(requestObject);
            try {
                Map<String,String> headers  =new HashMap<>();
                headers.put("Content-Type","application/json;charset=UTF-8");
                String post = HttpKit.post(url, str,headers);
                System.out.println("交易结果:"+post);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        /*
        *   {"logNo":"202212149242121974",
        *   "UserId":"ogDZBwK0jAgWgnppuW-L_MrxAKSo",
        *   "BalDate":"20221214",
        *   "AgentId":"810290000000003",
        *   "TxnLogId":"012084084951",
        *   "BusinessId":"800551000051931",
        *   "SDTermNo":"88145100",
        *   "TxnCode":"S004","PayChannel":"2",
        *   "TxnDate":"20221214","TxnTime":"084954",
        *   "TxnAmt":"314","TxnStatus":"1",
        *   "BankType":"","OfficeId":"","ChannelId":"",
        *   "attach":"","CrdFlg":"","batNo":"000022",
        *   "srefNo":"149242121974","feeAmt":"0.94",
        *   "channelOrderId":"","payTime":"","txn_cnl":"I",
        *   "card_type":"","txn_rsv1":"",
        *   "txn_rsv2":"","csn":"NLP200004587","payType":""}
        *
        * */


        return AjaxResult.success();
    }
}
